#include "MonsterWidget.h"
#include "ui_MonsterWidget.h"


MonsterWidget::MonsterWidget(MainWindow * mainWindow, Monster * monster, QWidget *parent) : QWidget(parent), ui(new Ui::MonsterWidget){
    ui -> setupUi(this);
    this -> monster = monster;
    this -> mainWindow = mainWindow;


    setMonsterType();
    setMonsterColor();
    setGlow(true);
    setOverlay(0);
    updateTooltip();

    connect(this -> monster, SIGNAL(signalStateChanged(bool)), this, SLOT(slotSateChanged(bool)));
    connect(this -> monster, SIGNAL(signalTimeBeforeRespawnChanged(int)), this, SLOT(slotTimeBeforeRespawnChanged(int)));
}



void MonsterWidget::updateTooltip(){
    this->setToolTip(this->monster->getName());
}





void MonsterWidget::setAlive(bool shouldWait){
    this->monster->setAlive();
    this->monster->exit(0);
    if (shouldWait)
        this->monster->wait();
}

void MonsterWidget::slotSateChanged(bool state){
    if (state){     //Now Alive
        setGlow(true);
        setOverlay(0);
        setText("");
        this->mainWindow->log("Monster", this->monster->getName() + " has respawned.");
    } else {
        setGlow(false);
        setOverlay(100);
        this->mainWindow->log("Monster", this->monster->getName() + " has been killed.");
    }
}

void MonsterWidget::slotTimeBeforeRespawnChanged(int timeBeforeRespawn){
    setTime(timeBeforeRespawn / 1000);
    setOverlay((timeBeforeRespawn) ? (this -> monster -> getRespawnTime() * 100 / timeBeforeRespawn) : 0);
}




void MonsterWidget::setMonsterColor(){
    this -> ui -> Border -> setStyleSheet("background-image:url(:/PNG/Resources/PNG/" + this->monster->getMonsterColor(this->monster->getMonsterColor()) + "Border.png);");
    this -> ui -> Glow -> setStyleSheet("background-image:url(:/PNG/Resources/PNG/" + this->monster->getMonsterColor(this->monster->getMonsterColor()) + "Glow.png);");
}






void MonsterWidget::kill(){
    this->monster->kill();
}


void MonsterWidget::setMonsterType(){
    this -> ui -> Image -> setStyleSheet("background-image:url(:/PNG/Resources/PNG/" + this -> monster -> getMonsterType(this->monster->getMonsterType()) + ".png);");
}


void MonsterWidget::setGlow(bool activate){
    if (activate){
        this->ui->Glow->show();
        this->ui->Shadow->hide();
    } else {
        this->ui->Glow->hide();
        this->ui->Shadow->show();
    }
}

void MonsterWidget::setOverlay(int percent){
    if (percent){
        this -> ui -> Overlay -> show();
        int overlayHeight = (this -> ui -> Border -> height() - 1) * 100 / percent;
        this -> ui -> Overlay -> resize(this->ui->Border->width(), overlayHeight);
        this->ui->Overlay->move(this->ui->Border->x(), this->ui->Border->y() + (this->ui->Border->height() - overlayHeight));
    } else {
        this -> ui -> Overlay -> hide();

    }
}


void MonsterWidget::setTime(int time){
    QString seconds = QString::number(time % 60);
    QString minutes = QString::number(time / 60);

    setText(((minutes.size() == 1) ? "0" + minutes : minutes) + ":" + ((seconds.size() == 1) ? "0" + seconds : seconds));

}



void MonsterWidget::setText(QString text){
    int fontSize = (int)((float) 50 * (float) 1 / (float) text.size());
    this->ui->Text->setText(text);
    this->ui->Text->setFont(QFont("Arial", fontSize, QFont::Bold));
    this->ui->TextShadow->setText(text);
    this->ui->TextShadow->setFont(QFont("Arial", fontSize, QFont::Bold));

}



MonsterWidget::~MonsterWidget(){
    this -> monster -> exit(0);
    this -> monster -> wait();
    delete monster;
    delete ui;
}
